/* eslint-disable no-restricted-globals */
/* eslint-disable no-continue */
/* eslint-disable eqeqeq */
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-plusplus */
function main(json) {
  const result = [];

  const recordMap = {};

  const head = json[0];

  let CURRENT;
  let beforeCURRENT;

  const transNum = (s) => {
    if (s == undefined) return '--';
    const val = `${s}`.trim();
    const num = Number(val);
    if (isNaN(num)) return '--';
    return Math.abs(num).toFixed(2);
  };
  function genStr(name, { beforePrice, price }) {
    if (!beforePrice && !price) return undefined;
    if (!price) return `Q${beforeCURRENT}价格：${transNum(beforePrice)}`;
    if (!beforePrice) return `Q${CURRENT}价格：${transNum(price)}`;
    const downPrice = parseFloat(beforePrice) - parseFloat(price);
    const dowRange = (downPrice / beforePrice) * 100;
    const isUp = downPrice < 0;
    return `Q${beforeCURRENT}价格：${transNum(
      beforePrice,
    )}; Q${CURRENT}价格：${transNum(price)}; ${isUp ? '涨价' : '降价'}：${transNum(
      downPrice,
    )}; ${isUp ? '涨幅' : '降幅'}：${transNum(dowRange)}%`;
  }

  // 扫描表头查出供应商
  let columnBegin = 0;
  for (let i = 0; i < head.length; i++) {
    const name = head[i];
    const match = name.match(/_Q\d/g);
    if (match && match.length) {
      const num = match[0].match(/\d/g)[0];
      if (beforeCURRENT == undefined) {
        beforeCURRENT = num;
        const bnum = head[i + 1].match(/\d/g)[0];
        CURRENT = bnum;
      }
    }
    const sourceName = name.split('_')[0];
    if (!sourceName || !match) {
      columnBegin = i + 1;
      continue;
    }
    if (!recordMap[sourceName]) {
      recordMap[sourceName] = {
        beforePriceIndex: -1,
        priceIndex: -1,
      };
    }
    if (!beforeCURRENT) continue;
    if (name.includes(`Q${beforeCURRENT}`)) {
      // 上月
      recordMap[sourceName].beforePriceIndex = i;
      // 当月
      recordMap[sourceName].priceIndex = i + 1;
    }
  }

  for (let i = 1; i < json.length; i++) {
    const columns = json[i].slice(0, columnBegin);
    const category = {};
    for (let j = columnBegin; j < json[i].length; j++) {
      for (const key in recordMap) {
        if (!category[key]) {
          category[key] = {
            price: undefined,
            beforePrice: undefined,
          };
        }
        if (j == recordMap[key].beforePriceIndex) {
          category[key].beforePrice = json[i][j];
        } else if (j == recordMap[key].priceIndex) {
          category[key].price = json[i][j];
        }
      }
    }
    result.push({
      columns,
      category,
    });
  }

  const sheet = [head.slice(0, columnBegin).concat(Object.keys(recordMap))];

  for (let i = 0; i < result.length; i++) {
    const item = result[i];
    const row = item.columns;
    for (const supplier in item.category) {
      const data = item.category[supplier];
      const str = genStr(supplier, data);
      if (str == undefined) {
        row.push('');
      } else {
        row.push(str);
      }
    }
    sheet.push(row);
  }

  return sheet;
}

export default main;
